package com.xxxx.controller;

import com.xxxx.pojo.Order;
import com.xxxx.service.DoorService;
import com.xxxx.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created By ShaoXikai on 2022/7/24
 */
@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    @Qualifier("doorServiceImpl")
    private DoorService doorService;

    @Autowired
    @Qualifier("orderServiceImpl")
    private OrderService orderService;

    @GetMapping("/list")
    public String list(Model model) {
        List<Order> orderList = orderService.findAllOrder();
        model.addAttribute("orderList", orderList);
        return "order_list";
    }

    @GetMapping("/toSave")
    public String toSave(Model model) {
        model.addAttribute("doorList", doorService.findAll());
        return "order_add";
    }

    @PostMapping("/save")
    public String save(Order order) {
        //System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date()));
        order.setOrderTime(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date()));
        order.setPayTime(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date()));
        orderService.saveOrder(order);
        return "redirect:/order/list";
    }

    @GetMapping("/remove/{id}")
    public String remove(@PathVariable long id) {
        orderService.remove(id);
        return "redirect:/order/list";
    }

    @GetMapping("/toUpdate/{id}")
    public String toUpdate(@PathVariable long id, Model model) {
        model.addAttribute("doorList", doorService.findAll());
        model.addAttribute("order", orderService.findOrderById(id));
        return "order_update";
    }

    @PostMapping("/update")
    public String update(Order order) {
        orderService.modifyOrder(order);
        return "redirect:/order/list";
    }
}
